import requests
from bs4 import BeautifulSoup
from prettytable import *
import random
import json
import calendar
import mysql
import time
import datetime
from datetime import timedelta


def get_url(url, params=None, proxies=None):
    user_agent_list = ["Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36",
                "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
                "Mozilla/5.0 (Windows NT 10.0;) Gecko/20100101 Firefox/61.0",
                "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36",
                "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36",
                "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)",
                "Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10.5; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15",
                ]

    random_agent = random.choice(user_agent_list)
    # print(random_agent)

    header = {
        'User-Agent' : random_agent,
        'Referer' : 'http://fund.eastmoney.com/'
    }
    # print(params)
    rsp = requests.get(url, params=params, proxies=proxies, headers=header, timeout=5)
    rsp.encoding = "utf-8"
    rsp.raise_for_status()
    return rsp.text


def get_fund_data(code):
    records = []
    record = {'Code': code}
    rt = random.random()

    url = "http://api.fund.eastmoney.com/f10/JJHSL/?callback=jQuery{rt}&fundcode={code}&pageindex=1&pagesize=20&_={rt}"
    url = url.format(code=code, rt=rt)
    # url = "http://fundgz.1234567.com.cn/js/{code}.js?rt={rt}".format(code=code, rt=random.random())
    
    params = {}

    try:
        json_str = get_url(url, params)
        
        # if len(json_str) < 20:
        #     return records
        startIdx = json_str.index("{\"Data\"")
        json_str = json_str[startIdx:len(json_str)-1]
        # print(json_str)

        json_obj = json.loads(json_str)
        # print(json_obj)

        for data in json_obj['Data']:
            record['REPORTDATE'] = data['REPORTDATE']
            record['STOCKTURNOVER'] = data['STOCKTURNOVER']
            records.append(record.copy())
    except Exception as e:
        print("出现如下异常%s"%e)


    return records



if __name__ == "__main__":

    today = datetime.datetime.now().strftime("%Y-%m-%d")

    # get_fund_data("004997")

    # Load All Fund's Code
    # fund_arr = mysql.getDiyFunds("SELECT fund.fundId FROM fund LEFT JOIN (SELECT DISTINCT fundId FROM jjhsl) hsl ON fund.fundId=hsl.fundId WHERE hsl.fundId is null")
    fund_arr = mysql.getDiyFunds("SELECT fund.fundId FROM fund WHERE fund.fundId='161005'")

    for fund in fund_arr:
        fundId = fund['Code']
        records = get_fund_data(fundId)
        if len(records) > 0:
            print("更新换手率：%s" % (fundId))
            mysql.updateFundHSL(fundId, records)
        else:
            print("没有查询到数据!")


    print("%s日期基金估值抓取完成" % (today))



